import { GeneraDataType } from "@/interface";
import { setAuthent } from "@/utils/auth";
import { Modal, message } from "ant-design-vue";
import { createVNode } from "vue";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
import { disabledMenuApi, enabledMenuApi } from "@/api/configCenter/menuManagement";
export default function OperationHook(state: GeneraDataType) {
  const { tableRef, expandRows, searchHandle } = state;
  const expandAllEvent = () => {
    const $table = tableRef.value.xTable;
    if ($table) {
      $table.setAllTreeExpand(true);
    }
  };

  const clearExpandEvent = () => {
    const $table = tableRef.value.xTable;
    if ($table) {
      $table.clearTreeExpand();
    }
  };
  const toggleExpandChangeEvent = (params: any) => {
    expandRows.value = params.$table.getTreeExpandRecords().map((t: any) => t.id);
    setAuthent("expandRows", JSON.stringify(expandRows.value));
  };
  const changeStatusHandle = (row: MenuManagement.ListRows) => {
    const params: any = {
      id: row.id
    };
    let tip = null,
      requestFn: any = null;
    if (row.status === 1) {
      tip = "是否停用该菜单/按钮?";
      requestFn = disabledMenuApi;
    } else {
      tip = "是否启用该菜单/按钮?";
      requestFn = enabledMenuApi;
    }
    Modal.confirm({
      title: tip,
      icon: createVNode(ExclamationCircleOutlined),
      content: "",
      okText: "确定",
      okType: "danger",
      cancelText: "取消",
      onOk() {
        requestFn(params).then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功!");
            searchHandle();
          }
        });
      }
    });
  };
  return {
    expandAllEvent,
    clearExpandEvent,
    toggleExpandChangeEvent,
    changeStatusHandle
  };
}
